/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.managedbeans;

import com.blog.city.checkthedep.entities.RegisteredUser;
import com.blog.city.checkthedep.sessionbeans.RegisteredUserFacadeLocal;
import java.awt.event.ActionEvent;
import javax.annotation.ManagedBean;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Breath
 */
@ManagedBean
@SessionScoped
public class LoginBean {

    private RegisteredUser checkedUser = new RegisteredUser();
    private String checkedUsername;
    private String checkedPassword;
    @EJB
    private RegisteredUserFacadeLocal regUserFa;

    /**
     * 
     * @return
     */
    public String getCheckedUsername() {
        return checkedUsername;
    }

    /**
     * 
     * @param checkedUsername
     */
    public void setCheckedUsername(String checkedUsername) {
        this.checkedUsername = checkedUsername;
    }

    /**
     * 
     * @return
     */
    public String getPassword() {
        return checkedPassword;
    }

    /**
     * 
     * @param password 
     */
    public void setPassword(String password) {
        this.checkedPassword = password;
    }

    /**
     * 
     * @return
     */
    public String getUsername() {
        return checkedUsername;
    }

    /**
     * 
     * @param username 
     */
    public void setUsername(String username) {
        this.checkedUsername = username;
    }

    /**
     * 
     */
    public void retrieveUser() {
        this.setCheckedUser(regUserFa.findRegUserByUsername(checkedUsername));
    }

    /**
     * 
     * @return
     */
    public RegisteredUser getCheckedUser() {
        return checkedUser;
    }

    /**
     * 
     * @param checkedUser
     */
    public void setCheckedUser(RegisteredUser checkedUser) {
        this.checkedUser = checkedUser;
    }

    /**
     * 
     * @return
     */
    public RegisteredUserFacadeLocal getRegUserFa() {
        return regUserFa;
    }

    /**
     * 
     * @param regUserFa
     */
    public void setRegUserFa(RegisteredUserFacadeLocal regUserFa) {
        this.regUserFa = regUserFa;
    }

    /**
     * 
     * @return
     */
    public void loginUser() {
        retrieveUser();
        boolean loggedIn = false;
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        if (checkedUser.getPassword().equals(checkedPassword)) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", checkedUser.getUsername());
            loggedIn = true;

        } else {
            loggedIn = false;
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("loggedIn", loggedIn);
       

    }

    /**
     * 
     * @return
     */
    public boolean logout() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("loginBean");
        return true;
    }

    /**
     * 
     * @return
     */
    public String redirection() {
        if (logout() == true) {
            return "showIndex";
        } else {
            return "";
        }
    }
}
